import re
import json
import requests
import json
import os
import imageio
import requests
from bs4 import BeautifulSoup
from retrying import retry

from utils import mongo_manager, proxies

tuchong_topic = mongo_manager("tuchong_topic", db='car_images')
tuchong_goods = mongo_manager("tuchong_goods", db='car_images')

headers = {
    "authority": "stock.tuchong.com",
    "accept": "application/json, text/plain, */*",
    "accept-language": "zh-CN,zh;q=0.9",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "referer": "https://stock.tuchong.com/topic?goodsType=0&page=2&size=100&topicId=52787",
    "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Linux\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
cookies = {
    "creative_device_id": "140b67fc-90e3-4701-8385-c50faf34c088",
    "Hm_lvt_f212e14a5ffb8199fd0e64061c054314": "1709186463",
    "Hm_lpvt_f212e14a5ffb8199fd0e64061c054314": "1710228200"
}


def get_topic_id(url="https://stock.tuchong.com/api/search/topic", params=None):
    if params == None:
        params = {
            "goods_type": "0",
            "need_goods_id_count": "4",
            "page": "1",
            "size": "60",
            "sort_by": "1"
        }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)

    print(response.url, response)
    if response.status_code == 200:
        return response.json()['data']['hits']


def get_topic_data(url="https://stock.tuchong.com/api/topic", params=None):
    if params is None:
        params = {
            "goods_type": "0",
            "page": "2",
            "size": "100",
            "topic_id": "52787"
        }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)
    print(response.url, response)
    if response.status_code == 200:
        return response.json()['data']['goods_list']


def get_topic_data2(url="https://stock.tuchong.com/topic"):
    params = {
        "goodsType": "0",
        "topicId": "52576"
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)
    print(response.text)
    # 匹配开头是"topic = {"的一行
    regex_topic = r'topic=(\{.*?\});'
    # 匹配开头是"goods =["的一行
    regex_goods = r'goods=(\[\{.*?\}\]);'

    # 执行匹配
    match_topic = re.search(regex_topic, response.text)
    match_goods = re.search(regex_goods, response.text)

    # 输出结果
    print("Topic数据:")
    print(match_topic.group(1) if match_topic else "未找到")

    print("Goods数据:")
    print(match_goods.group(1) if match_topic else "未找到Goods数据")


# 获取所有topic id
def main1():
    for goods_type in ['0', '1', '2']:
        page = 1
        while True:
            print("正在抓取第%d页..." % page)
            params = {
                "goods_type": goods_type,
                "need_goods_id_count": "4",
                "page": page,
                "size": "60",
                "sort_by": "1"
            }
            hits = get_topic_id(params=params)
            if hits:
                for hit in hits:
                    hit["_id"] = hit['id']
                    try:
                        tuchong_topic.insertOne(hit)
                    except Exception as e:
                        print(e)
            else:
                break
            if len(hits) < 60:
                break
            page += 1


# 获取所有good id
def main2():
    seeds = tuchong_topic.findAll({'status': None,'goods_type':0})
    for seed in seeds:
        print("正在抓取topic id为%s的数据..." % seed['_id'],seed['title'])
        page = 1
        while True:
            params = {
                "page": page,
                "size": "100"
            }
            params["topic_id"] = seed['_id']
            params["goods_type"] = seed['goods_type']
            goods_list = get_topic_data(params=params)
            if goods_list:
                for goods in goods_list:
                    goods['_id'] = goods['image_id']
                    try:
                        tuchong_goods.insertOne(goods)
                    except Exception as e:
                        print(e)
            else:
                break
            if len(goods_list) < 100:
                break
            page += 1
        seed['status'] = 'done'
        tuchong_topic.updateOne({'_id': seed['_id']}, seed)

# 获取image id 后 url的构造方式
# https://stock.tuchong.com/image/detail?imageId=1902326751569379339&platform=image&term=&requestId=&searchId=&page=1&entryFrom=%E4%B8%93%E9%A2%98%E5%88%97%E8%A1%A8&index=0
def get_image_data(url = 'https://stock.tuchong.com/image/detail',params =None):
    if params  is None:
        params = {
            "imageId": "1902326751569379339",
            "platform": "image",
            "term": "",
            "requestId": "",
            "searchId": "",
            "page": "1",
            "entryFrom": "专题列表",
            "index": "0"
        }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)

    print(response.url,response)
    if response.status_code == 200:
        return response.text
if __name__ == '__main__':
    print()
    hits = get_topic_id()
    print(hits)

    # main2()
    # response = get_image_data()
    # print(response)
